﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using edowork.com.SQL2005;
using edowork.com.Model;


namespace WebInvoicingsys.system.baseset.basedata
{
    public partial class logistics_add : System.Web.UI.Page
    {
        //前台输出提示信息变量
        public string alert_msg = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ;
            }
        }

        //保存
        protected void btnSave_Click(object sender, EventArgs e)
        {
            alert_msg = "";

            edowork.com.Model.logisticsInfo logisticsInfo = new logisticsInfo();

            logisticsInfo.Logisticsid = txt_logisticsid.Text.Trim();
            logisticsInfo.Logisticsname = txt_logisticsname.Text.Trim();
            logisticsInfo.Phone = txt_phone.Text.Trim();
            logisticsInfo.Contact = txt_contact.Text.Trim();
            logisticsInfo.Url = txt_url.Text.Trim();
            logisticsInfo.Sysdate = DateTime.Now.ToString("yyyy-MM-dd");


            if (String.IsNullOrEmpty(logisticsInfo.Logisticsid))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请输入物流商编码");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                txt_logisticsid.Focus();

                return;
            }

            if (String.IsNullOrEmpty(logisticsInfo.Logisticsid))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请输入物流商名称");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                txt_logisticsname.Focus();

                return;
            }

            if (String.IsNullOrEmpty(logisticsInfo.Phone))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请输入联系电话");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                txt_phone.Focus();

                return;
            }

            //保存数据入数据库
            using (edowork.com.SQL2005.TranUtil tranutil = new TranUtil(Session["connectionString"].ToString()))
            {
                System.Data.SqlClient.SqlTransaction tran = null;

                try
                {
                    tran = tranutil.GetSqlTransaction();
                }
                catch (Exception er)
                {
                    alert_msg = String.Format(Scripts.alert_warning, er.Message);

                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                    return;
                }

                edowork.com.SQL2005.Execute exec = new Execute(Session["connectionString"].ToString());

                string sql = "";
                int result = -1;

                //保存入logistics表
                sql = String.Format(logisticsInfo.SQL_Insert(), logisticsInfo.Logisticsid, logisticsInfo.Logisticsname, logisticsInfo.Phone, logisticsInfo.Contact, logisticsInfo.Url, logisticsInfo.Sysdate);

                result = exec.ExecSQLQueryInt(sql, ref alert_msg, tran);

                if (result <= 0)
                {
                    tran.Rollback();
                    tranutil.CloseConnection();

                    alert_msg = String.Format(Scripts.alert_warning, alert_msg);

                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                    return;
                }

                tran.Commit();
                tranutil.CloseConnection();
            }

            txt_logisticsid.Text = "";
            txt_logisticsname.Text = "";
            txt_phone.Text = "";
            txt_contact.Text = "";
            txt_url.Text = "";


            alert_msg = String.Format(Scripts.alert_success, Errormsg.success_Save);

            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
        }

        //重置
        protected void btnReset_Click(object sender, EventArgs e)
        {
            txt_logisticsid.Text = "";
            txt_logisticsname.Text = "";
            txt_phone.Text = "";
            txt_contact.Text = "";
            txt_url.Text = "";
        }

        //返回
        protected void btnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect("/system/baseset/basedata/logistics.aspx");
        }
    }
}